package com.ruoyi.web.controller.tech;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.ProductUpdateStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.system.domain.Technician;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ITechnicianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

@Anonymous  //此注解是不加权限直接可以调用该类的所有接口
@RestController
@CrossOrigin //这里是解决前端跨域的问题
@RequestMapping("/company/technician")
public class TechnicianController extends BaseController {

    @Autowired
    private ITechnicianService technicianService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 列表
     */
    //http://127.0.0.1:9070/company/technician/list
    @GetMapping("/list")
    public TableDataInfo list(Technician technician) {
        startPage();
        List<Technician> list = technicianService.selectAllTechnician(technician);
        return getDataTable(list);
    }

    /**
     * 根据id获取详细信息
     */
    //http://127.0.0.1:9070/company/technician/1
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(technicianService.selectTechnicianById(id));
    }

    /**
     * 新增人员
     */
    //http://127.0.0.1:9070/company/technician/add
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Technician technician) {
        technician.setCreateBy(getUsername());
        return toAjax(technicianService.insertTechnician(technician));
    }

    /**
     * 修改人员状态
     */
    //http://127.0.0.1:9070/company/technician/update
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody Technician technician) {
        technician.setUpdateBy(getUsername());
        return toAjax(technicianService.updateTechnician(technician));
    }

    /**
     * 删除人员
     */
    //http://127.0.0.1:9070/company/technician/11
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        technicianService.deleteTechnicianByIds(ids);
        return success();
    }

    /**
     * 修改人员状态
     */
    //http://127.0.0.1:9070/company/technician/updateStatus
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@Validated @RequestBody Technician technician) {
        if (!"1".equals(technician.getStatus())) {
            // 下架的设置为不在首页展示
            technician.setIfIndex("0");
        }
        int i = technicianService.updateStatus(technician);
        return toAjax(i);
    }

    @PutMapping("/changeIfIndex")
    public AjaxResult changeIfIndex(@Validated @RequestBody Technician technician) {
        int i = technicianService.updateIfIndex(technician);
        return toAjax(i);
    }

    @PostMapping("/batchUpdateStatus")
    public AjaxResult batchUpdateStatus(@RequestBody ProductUpdateStatus productUpdateStatus) {
        List<Long> ids = productUpdateStatus.getIds();
        Technician technician = technicianService.selectTechnicianById(ids.get(0));
        if (StringUtils.isEmpty(ids)) {
            return error("待更新状态为空");
        }
        List<SysDictData> sysDictDatas = sysDictTypeService.selectDictDataByType("business_status");
        if (sysDictDatas.stream().noneMatch(sysDictData -> Objects.equals(sysDictData.getDictValue(), productUpdateStatus.getStatus()))) {
            return error(String.format("状态值:{}非法", productUpdateStatus.getStatus()));
        }
        int i = technicianService.batchUpdateStatus(productUpdateStatus);
        return success(StringUtils.format("成功更新{}条数据", i));
    }

    /**
     * 上传产品图片(不修改数据库，单纯地获取到上传后的图片URL)
     *
     * @param file 文件
     * @return {@link AjaxResult}
     * @throws Exception 异常
     */
    @PostMapping("/uploadMainImg")
    public AjaxResult uploadMainImg(@RequestParam("mainImg") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                String imgUrl = FileUploadUtils.upload(RuoYiConfig.getTechnicianUploadPath1(), file, MimeTypeUtils.IMAGE_EXTENSION);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", imgUrl);
                return ajax;
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
        }
        return error("上传异常，请联系管理员");
    }

    /**
     * 上传产品图片(不修改数据库，单纯地获取到上传后的图片URL)
     *
     * @param file 文件
     * @return {@link AjaxResult}
     * @throws Exception 异常
     */
    @PostMapping("/uploadListImg")
    public AjaxResult uploadListImg(MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                String imgUrl = FileUploadUtils.upload(RuoYiConfig.getTechnicianUploadPath1(), file, MimeTypeUtils.IMAGE_EXTENSION);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", imgUrl);
                return ajax;
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
        }
        return error("上传异常，请联系管理员");
    }

}
